package com.hstcs.assets;

import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.hstcs.components.MgComponent;
import com.hstcs.components.TransformComponent;

public class Area {
	public static Rectangle maoBound = new Rectangle();
	public static Rectangle gouBound = new Rectangle();
	
	
	public static int _MAO_GROUND = 1;
	public static int _GOU_GROUND = 2;
	public static int _CO_GROUND  = 3;
	
	public static int WIDTH = 200;
	private static Vector2 tmpVector2 = new Vector2();

	public static void init(){
		maoBound.x = Config._WIDTH - WIDTH/2;
		maoBound.y = Config._HEIGHT/2;
		maoBound.setWidth(WIDTH);
		maoBound.setHeight(WIDTH);
		
		
		gouBound.x = WIDTH/2;
		gouBound.y = Config._HEIGHT/2;
		gouBound.setWidth(WIDTH);
		gouBound.setHeight(WIDTH);
	}
	
	public int local(Vector2 pos){
		
		
		return 0;
	}
	
	public static void  setMgDest(Vector2 pos){
		
		int x = MathUtils.random(WIDTH, (int)Config._WIDTH - WIDTH);
		int y = MathUtils.random(0, (int)Config._HEIGHT);
		pos.set(x, y);
	}
	
	public static void setMgDest(Vector2 srcPos, Vector2 pos){
		int  y = 0;
		int  x = 0;
		if(srcPos.y > Config._HEIGHT/2 + WIDTH/2
				&&(srcPos.x < WIDTH 
				||srcPos.x > Config._WIDTH - WIDTH) )
		{
			y = (int)MathUtils.random(Config._HEIGHT/2 + WIDTH/2, Config._HEIGHT);
			x = (int)MathUtils.random(Config._WIDTH);
		}else  if(srcPos.y > Config._HEIGHT/2 + WIDTH/2
				&& srcPos.x > WIDTH
				&& srcPos.x < Config._WIDTH - WIDTH)
		{
			int tmp = MathUtils.random(2);
			if(tmp == 2 ){
				y = (int)MathUtils.random(Config._HEIGHT/2 + WIDTH/2, Config._HEIGHT);
				x = (int)MathUtils.random(Config._WIDTH);
			}else{
				y = (int)MathUtils.random(Config._HEIGHT);
				x = (int)MathUtils.random(WIDTH, Config._WIDTH - WIDTH);
			}
		}else  if(srcPos.y < Config._HEIGHT/2 - WIDTH/2
				&& srcPos.x > WIDTH
				&& srcPos.x < Config._WIDTH - WIDTH)
		{
			int tmp = MathUtils.random(2);
			if(tmp == 2 ){
				y = (int)MathUtils.random(Config._HEIGHT/2 - WIDTH/2);
				x = (int)MathUtils.random(Config._WIDTH);
			}else{
				y = (int)MathUtils.random(Config._HEIGHT);
				x = (int)MathUtils.random(WIDTH, Config._WIDTH - WIDTH);
			}
			
		}else  if(srcPos.y < Config._HEIGHT/2 - WIDTH/2
				&& (srcPos.x < WIDTH
				|| srcPos.x > Config._WIDTH - WIDTH))
		{
			y = (int)MathUtils.random(Config._HEIGHT/2 - WIDTH/2);
			x = (int)MathUtils.random(Config._WIDTH);
		}else{
			y = (int)MathUtils.random(Config._HEIGHT);
			x = (int)MathUtils.random(WIDTH, Config._WIDTH - WIDTH);
		}
			
		pos.set(x, y);
		if(isXGround(x, y) == _MAO_GROUND 
		  || isXGround(x, y) == _GOU_GROUND)
		{
			System.out.println("x:"+ x +"===y:"+y + " srcPos:" + srcPos + "ppppt:" );
		}
	}
	
	public static void setMgDest(Vector2 pos, int type){
		int x = 0;
		if(type == _MAO_GROUND){
			 x = MathUtils.random(0, WIDTH);
		}else if(type == _GOU_GROUND){
			 x = (int)MathUtils.random(Config._WIDTH - WIDTH, Config._WIDTH);
		}
		int y = (int)MathUtils.random((Config._HEIGHT - WIDTH)/2, (Config._HEIGHT + WIDTH)/2);
		pos.set(x, y);
	}
	
	public static void setMgBornPos(Vector3 pos){
		int rand = MathUtils.random(1);
		int x = MathUtils.random(WIDTH, (int)Config._WIDTH - WIDTH);
		int y = 0;
		if(rand == 0){
			y = (int)Config._HEIGHT;
		}
		pos.x = x;
		pos.y = y;
	}
	
	public static int isXGround(float x, float y){
		if(x < WIDTH&&y > ((Config._HEIGHT - WIDTH)/2) && y <((Config._HEIGHT + WIDTH)/2) ) return _MAO_GROUND;
		if(x > Config._WIDTH - WIDTH && y > ((Config._HEIGHT - WIDTH)/2)&& y <((Config._HEIGHT + WIDTH)/2)) return _GOU_GROUND;
		return _CO_GROUND;
	}
	public static void test(MgComponent mc, TransformComponent tc ){
		if(isXGround(tc.pos.x, tc.pos.y) == _MAO_GROUND 
		  || isXGround(tc.pos.x, tc.pos.y) == _GOU_GROUND)
		{
			System.out.println("srcPos:" + mc.srcPos+ "ppppppppp des:"+ mc.dest );
		}
	}
}
